from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLineEdit, QTextEdit, QPushButton, QMessageBox, QHBoxLayout
from datetime import datetime, timedelta
from config import button_config

class Blacklist(QDialog):
    def __init__(self, parent=None):
        super(Blacklist, self).__init__()
        self.parent = parent
        self.user1id=self.parent.client_id
        self.user2id=self.parent.user_id
        self.client=self.parent.client
        self.setWindowTitle('添加用户到黑名单')
        self.resize(400,400)
        main_layout = QVBoxLayout(self)
        self.initUI(main_layout)
    def initUI(self,main_layout):
        self.input_date2=QLineEdit()
        self.input_date2.setPlaceholderText('你想将他加入黑名单几天')
        main_layout.addWidget(self.input_date2)
        self.res_input=QTextEdit()
        self.res_input.setPlaceholderText('加入黑名单的原因')
        self.res_input.setFixedHeight(160)
        main_layout.addWidget(self.res_input)
        layout=QHBoxLayout()
        main_layout.addLayout(layout)
        submit_btn=QPushButton()
        submit_btn.setText('确认')
        layout.addWidget(submit_btn)
        submit_btn.setStyleSheet(button_config.button_style2)
        submit_btn.clicked.connect(self.submit)
        exit_btn=QPushButton()
        exit_btn.setText('退出')
        layout.addWidget(exit_btn)
        exit_btn.setStyleSheet(button_config.button_style4)
        exit_btn.clicked.connect(self.exit)

    def submit(self):
        res=QMessageBox.warning(None,'警告','你确定要拉黑该用户吗',QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
        if res==QMessageBox.Yes:
            day=self.input_date2.text()
            try :
                a=int(day)
            except :
                QMessageBox.critical(None,'错误','请输入正确的格式')
                return
            date1=datetime.now()
            date2=date1+timedelta(days=a)
            date1=date1.strftime('%Y-%m-%d')
            date2=date2.strftime('%Y-%m-%d')
            reason=self.res_input.toPlainText()
            parser={
                'date1':date1,
                'date2':date2,
                'reason':reason,
                'user1id':self.user1id,
                'user2id':self.user2id,
                'statu':True
            }
            self.client.update_blacklist(parser)



    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()

